<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class ExaminerController extends BaseController {

    //考官列表
	public function index() {
        //筛选条件
        $data = array_filter($_GET);
        if (!empty($_GET['user_nickname'])) {
            $data['user_nickname'] = array('like',"%".trim($_GET['user_nickname'])."%");
        }
        if (!empty($_GET['user_phone'])) {
            $data['user_phone'] = array('like',"%".trim($_GET['user_phone'])."%");
        }
        unset($data['p']);

        $data['user_display'] = 1;
        $count = M('user')->where($data)->where('user_role = 3 or user_role = 4')->count();
        $page = getpage($count,10);
        $user_arr = M('user')->where($data)->where('user_role = 3 or user_role = 4')->order('user_id desc')->limit($page->firstRow, $page->listRows)->select();
        $this->assign('user_arr', $user_arr);
        $this->assign('page', $page->show());
        $this->display();
    }

    //删除考官
    public function examiner_del(){
        $data['user_id'] = I('get.user_id');
        $data['user_display'] = 0;
        if(M('user')->save($data)){
            $this->redirect('index','删除考官成功');
        }else{
            $this->error('删除考官失败');
        }
    }

    //考官详情
    public function examiner_details(){
        $data['user_id'] = I('get.user_id');
        $user_arr = M('user')->alias('u')->join('left join midi_city as c on u.user_city = c.city_id')->where($data)->find();
        $this->assign('user_arr',$user_arr);
        $this->display();
    }

    //添加考官
    public function examiner_add(){
        if (I('post.action') == 'examiner_add_action') {
            $this->examiner_add_action();
            exit();
        }
        $city_arr = M('city')->select();
        $this->assign('city_arr', $city_arr);
        $this->display();
    }

    //添加考官操作
    public function examiner_add_action(){
    	if (empty($_POST['user_income_proportion'])) {
    		$this->error('考官收入比例不能为空！');
    	}elseif ($_POST['user_income_proportion'] >= 1) {
    		$this->error('考官收入比例不能超过1');
    	}
        if($_POST['addaction'] == 'examiner_add') {
            unset($_POST['addaction']);
            $data['user_id'] = $_POST['user_id'];
            if ($user = M('user')->where($data)->find()) {
                if($user['user_role'] == 3 || $user['user_role'] == 4){
                    $this->error('该用户已经是考官');
                }elseif($user['user_role'] == 1){
                    $_POST['user_role'] = 4;
                    if (M('user')->save($_POST)) {
                        $this->redirect('index','添加考官成功！');
                    }else{
                        $this->error('添加考官失败！');
                    }
                }elseif($user['user_role'] == 2){
                    $_POST['user_role'] = 3;
                    if (M('user')->save($_POST)) {
                        $this->redirect('index','添加考官成功！');
                    }else{
                        $this->error('添加考官失败！');
                    }
                }
            }else{
                $this->error('midiID不存在');
            }
            exit();
        }
        $data['user_phone'] = $_POST['user_phone'];
        $user = M('user')->where($data)->find();
        if(!empty($user)){
            $this->error('此手机号已经被注册过');
        }
        $finfo = upload($_FILES['avatar']);
        if ($finfo['status'] == 0 && $finfo['msg'] != 'nofile') {
            $this->error($finfo['msg']);
        } elseif ($finfo['status'] == 1) {
            $_POST['user_avatar'] = $finfo['filepath'];
        }
        $arr = array_filter($_POST);
        $arr['user_role'] = 4;
        if (!empty($_POST['user_password'])) {
            $arr['user_password'] = strtoupper(md5($_POST['user_password']));
        }
        $arr['user_reg_time'] = date('Y-m-d H:i:s',time());
        if(M('user')->add($arr)){
            $this->redirect('index','添加考官成功');
        }else{
            $this->error('添加考官失败');
        }

    }

    //修改考官信息
    public function examiner_edit(){
        if (I('post.action') == 'examiner_edit_action') {
            $this->examiner_edit_action();
            exit();
        }
        $data['user_id'] = I('get.user_id');
        $user_arr = M('user')->alias('u')->join('left join midi_city as c on u.user_city = c.city_id')->where($data)->find();
        $city_arr = M('city')->select();
        $this->assign('city_arr', $city_arr);
        $this->assign('user_arr',$user_arr);
        $this->display();
    }

    //修改考官信息操作
    public function examiner_edit_action(){
        $finfo = upload($_FILES['avatar']);
        if ($finfo['status'] == 0 && $finfo['msg'] != 'nofile') {
            $this->error($finfo['msg']);
        } elseif ($finfo['status'] == 1) {
            $_POST['user_avatar'] = $finfo['filepath'];
        }
        $_POST['user_examiner_guitar'] = isset($_POST['user_examiner_guitar'])?$_POST['user_examiner_guitar']:0;
        $_POST['user_examiner_drum'] = isset($_POST['user_examiner_drum'])?$_POST['user_examiner_drum']:0;
        $_POST['user_examiner_bass'] = isset($_POST['user_examiner_bass'])?$_POST['user_examiner_bass']:0;
        if(M('user')->save($_POST)){
            $this->success('修改信息成功');
        }else{
            $this->error('没有信息被修改');
        }
    }

}